/**
 * 
 */
package com.ermax.aircond.common.dao;

import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.ermax.aircond.common.domain.ReadMachineData;
import com.ermax.aircond.util.dao.AbstractHibernateDao;

/**
 * @author Eric, Maxwell
 * 
 */
@Repository("readMachineDataDao")
public class ReadMachineDataDao extends AbstractHibernateDao<ReadMachineData> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1418603846702420259L;
	private static final Logger logger = Logger.getLogger(ReadMachineDataDao.class);

	public ReadMachineDataDao() {
		setClazz(ReadMachineData.class);
	}

	@SuppressWarnings("unchecked")
	public ReadMachineData findMachineDateByNid(int nid) {
		Criteria criteria = this.getCurrentSession().createCriteria(ReadMachineData.class);
		criteria.add(Restrictions.eq("nid", nid));
		List<ReadMachineData> readMachineDatas = criteria.list();
		if (readMachineDatas.size() > 0) {
			return readMachineDatas.get(0);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public ReadMachineData getByNid(int nid) {

		Criteria criteria = getCurrentSession().createCriteria(ReadMachineData.class);
		if (isNotNull(nid)) {
			criteria.add(Restrictions.eq("nid", nid));
		}

		List<ReadMachineData> results = criteria.list();

		if (results.size() > 0) {
			return results.get(0);
		}

		return null;

	}

	public ReadMachineData getById(int id) {
		return (ReadMachineData) this.getCurrentSession().get(ReadMachineData.class, id);
	}

	public void updateRcmd(int nid) {

		logger.debug("Set SData.rcmd to 0 for nid:" + nid + "!");
		Query q = this.getCurrentSession().createSQLQuery("Update dbo.SData set dbo.SData.rcmd=0 from dbo.SData where dbo.SData.nid= :nid");
		q.setParameter("nid", nid);

		q.executeUpdate();
	}

	public List<ReadMachineData> getErrorNidData(String errorSignal) {

		logger.debug("Get SData.data which contains " + errorSignal + "!");
		/*
		 * Query q = this.getCurrentSession().createSQLQuery(
		 * "select * from dbo.SData where data like '%:errorSignal%'	");
		 * q.setParameter("errorSignal", errorSignal);
		 * 
		 * q.executeUpdate();
		 */

		Criteria criteria = getCurrentSession().createCriteria(ReadMachineData.class);
		if (isNotNull(errorSignal)) {
			criteria.add(Restrictions.like("data", "%" + errorSignal + "%"));
		}

		List<ReadMachineData> results = criteria.list();

		return results;
	}

	public int getMaxReadMachineDataIdx() {
		/*
		 * String sql = "SELECT MAX(IDX)from DataInterface.dbo.SData"; Query
		 * query = this.getCurrentSession().createSQLQuery(sql);
		 * 
		 * @SuppressWarnings("unchecked") List recordList = query.list();
		 * 
		 * if (null != recordList && recordList.size() > 0) {
		 * 
		 * return (Integer) recordList.get(0); }
		 */
		Criteria criteria = getCurrentSession().createCriteria(ReadMachineData.class);
		ProjectionList projectionList = Projections.projectionList();
		projectionList.add(Projections.max("idx"));
		criteria.setProjection(projectionList);
		List results = criteria.list();

		if(results != null && results.size() > 0){
			Object obj = results.get(0);
			if(obj != null){
				int idx = (Integer) results.get(0);
				return idx;
			}
		}		

		return 0;
	}

}
